package la.foton.atividade.converter;

import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import la.foton.atividade.entity.CasoDeUso;

@FacesConverter("casodeuso.converter")
public class CasoDeUsoConverter implements Converter
{

   @Override
   public Object getAsObject(FacesContext context, UIComponent component, String value)
   {
      if (value != null && !"".equals(value))
      {
         return this.getAttributesFrom(component).get(value);
      }
      return null;
   }

   @Override
   public String getAsString(FacesContext context, UIComponent component, Object value)
   {
      if (value != null && !"".equals(value))
      {
         CasoDeUso entity = (CasoDeUso) value;

         if (entity.getId() != null)
         {
            this.addAttribute(component, entity);

            if (entity.getId() != null)
            {
               return String.valueOf(entity.getId());
            }
            return (String) value;
         }
      }
      return "";
   }

   private void addAttribute(UIComponent component, CasoDeUso o)
   {
      this.getAttributesFrom(component).put(o.getId().toString(), o);
   }

   private Map<String, Object> getAttributesFrom(UIComponent component)
   {
      return component.getAttributes();
   }

}
